class Solution {
public:
    bool hasPathSum(TreeNode* root, int targetSum) {
        if (!root) return false;
        queue<TreeNode *> qPath;
        queue<int> sumPath;
        qPath.push(root);
        sumPath.push(0);
        while (!qPath.empty()) {
            int size = qPath.size();
            for (int i = 0; i < size; ++i) {
                TreeNode *node = qPath.front();
                qPath.pop();
                int curSum = sumPath.front();
                sumPath.pop();
                curSum += node->val;
                if (!node->left && !node->right) {
                    if (curSum == targetSum) return true;
                    continue;
                }
                if (node->left) {
                    qPath.push(node->left);
                    sumPath.push(curSum);
                }
                if (node->right) {
                    qPath.push(node->right);
                    sumPath.push(curSum);
                }
            }
        }
        return false;
    }
};

class Solution2 {
public:
    bool hasPathSum(TreeNode* root, int targetSum) {
        if (!root) return false;
        if (!root->left && !root->right)
            return targetSum == root->val;
        else {
            int temp = targetSum - root->val;
            return hasPathSum(root->left, temp) || hasPathSum(root->right, temp);
        }
    }
};